#!/usr/bin/env python
# encoding: utf-8

from django.views.generic.edit import FormView
from account.user.forms import LoginForm
from django.contrib.auth import authenticate, login, logout
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import View

import logging
logger = logging.getLogger("myself")


# 用户登录
class UserLoginView(FormView):
    template_name = "login.html"
    # success_url = '/dashboard'
    form_class = LoginForm
    def form_valid(self, form):
            data = form.cleaned_data
            user = authenticate(username=data['username'], password=data['password'])
            if user is not None:
                login(self.request,user)
                logger.debug("登录成功")
                return HttpResponseRedirect(reverse('index'))
                # return super(ContactView, self).form_valid(form)
            else:
                logger.debug("账号或密码不正确")
                return HttpResponseRedirect(reverse('login'))

# 用户推出类
class UserLogoutView(View):
    def get(self, request):
        logout(request)
        return HttpResponseRedirect(reverse("login"))

